package cn.elead.it.sso.system.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.gitee.elead.web.api.ApiResponses;
import com.gitee.elead.web.controller.SuperController;

import cn.elead.it.sso.system.model.Dept;
import cn.elead.it.sso.system.model.query.DeptQuery;
import cn.elead.it.sso.system.service.IDeptService;
import cn.elead.it.sso.system.service.dto.DeptDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


/**
 * <p>
 * 部门信息相关接口
 * </p>
 *
 * @author luopeng
 */
@RestController
@Api(tags = { "Dept" }, description = "部门信息相关接口")
@RequestMapping(value = "/api/dept", produces = MediaType.APPLICATION_JSON_VALUE)
public class DeptController extends SuperController{

    @Autowired
    private IDeptService deptService;
    
    /**
    * 查询部门信息信息
    * 
    * @param name  名称
    * @param status  状态
    * @return List<Dept>
    */    
    @ApiOperation("查询部门信息信息")
    @GetMapping
    @ApiImplicitParams({ //
        @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),//
        @ApiImplicitParam(name = "status", value = "状态", paramType = "query")//
    }) //
    public ApiResponses<List<DeptDTO>> findDepts(DeptQuery query){//状态
        return findList(query);
    }

    /**
    * 查询部门信息信息集合
    * 
    * @param name  名称
    * @param status  状态
    * @return List<Dept>
    */ 
    @ApiOperation("查询部门信息信息集合")
    @GetMapping("/list")
    @ApiImplicitParams({ //
         @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),//
        @ApiImplicitParam(name = "status", value = "状态", paramType = "query")//
    }) //
    public ApiResponses<List<DeptDTO>> findList(DeptQuery query){//状态
        return success(deptService.list(query));
    }

    /**
    * 分页查询部门信息信息
    * 
    * @param name  名称
    * @param status  状态
    * @return IPage<Dept>
    */ 
    @ApiOperation("分页查询部门信息信息")
    @GetMapping("/list/page")
    @ApiImplicitParams({ //
        @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),//
        @ApiImplicitParam(name = "status", value = "状态", paramType = "query"),//
        @ApiImplicitParam(name = "current", value = "当前页", paramType = "query"), //
        @ApiImplicitParam(name = "limit", value = "每页大小(默认每页条目20,最大条目数100)", paramType = "query") }) //        
    public ApiResponses<IPage<Dept>> findPageList(//
        @RequestParam(value = "name", required = false) String name,//名称
        @RequestParam(value = "status", required = false) Integer status){//状态
        
        return success(deptService.query() //分页查询
            .eq(ObjectUtils.isNotEmpty(name), Dept::getName, name)//名称
            .eq(ObjectUtils.isNotEmpty(status), Dept::getStatus, status)//状态
            .page(this.getPage()));
    }    

    /**
    * 根据ID查询部门信息
    * 
    * @param id  部门信息Id
    * @return Dept
    */ 
    @ApiOperation(value = "根据ID查询部门信息")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "部门信息id", required = true, paramType = "path") })
    @GetMapping("/{id}")
    public ApiResponses<Dept> findById(@PathVariable("id") String id) {
        return success(deptService.getById(id));
    }    

    /**
    * 添加部门信息信息
    * 
    * @param Dept  部门信息
    * @return Dept
    */ 
    @ApiOperation(value = "添加部门信息信息")
    @PostMapping
    public ApiResponses<Void> create(@RequestBody Dept Dept) {
        Dept.setId(StringUtils.EMPTY);
        deptService.save(Dept);
        return success(HttpStatus.CREATED);
    }

    /**
    * 修改部门信息信息
    * 
    * @param id  部门信息Id
    * @param Dept  部门信息
    * @return Dept
    */ 
    @ApiOperation(value = "修改部门信息信息")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "部门信息id", required = true, paramType = "path") })
    @PutMapping("/{id}")
    public ApiResponses<Void> update(@PathVariable("id") String id, @RequestBody Dept Dept) {
        Dept.setId(id);
        deptService.updateById(Dept);
        return success();
    } 

    /**
    * 删除部门信息信息
    * 
    * @param id  部门信息Id
    * @return 
    */ 
    @ApiOperation(value = "删除部门信息信息")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "部门信息id", required = true, paramType = "path") })
    @DeleteMapping("/{id}")
    public ApiResponses<Void> delete(@PathVariable("id") String id) {
        deptService.removeById(id);
        return success(HttpStatus.NO_CONTENT);
    }
    
	
	//@Scheduled(cron = "0 30 4 * * ?")
	@GetMapping("/init")
	public void init() {
		deptService.init();
	}
 }    

 